package com.zsj.LinkedList;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @version 1.0
 * @Author zsj
 * @Date 2023/10/22 14:28
 * @Descripiton 双向链表
 * 带哨兵
 */
public class DoublyLinkedList implements Iterable<Integer> {

    private Node head = null;
    private Node tail = null;


    public DoublyLinkedList() {
        head = new Node(8080, null, null);
        tail = new Node(9090, null, null);
        head.next = tail;
        tail.pre = head;
    }

    /**
     * 节点内部实现类
     */
    private static class Node {
        int value;//值
        Node pre;//上一个节点
        Node next;//下一个节点

        public Node(int value, Node pre, Node next) {
            this.value = value;
            this.pre = pre;
            this.next = next;
        }

    }


    /**
     * 将元素插入链表头部
     */
    public void addFirst(int value) {
        insert(0, value);
    }

    /**
     * 正常插入到尾部
     */
    public void addLast(int value) {
        Node node = new Node(value, tail.pre, tail);
        tail.pre.next = node;
        tail.pre = node;
    }

    /**
     * 寻找最后一个节点
     */
    private Node findLastNode() {
        return tail.pre;
    }

    /**
     * 获取索引位置的值
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new IllegalArgumentException("index by node is empty");
        }
        return node.value;
    }

    /**
     * 根据索引获取节点
     */
    private Node findNode(int index) {
        int begin = -1;
        for (Node p = head; p != tail; p = p.next, begin++) {
            if (begin == index) {
                return p;
            }
        }
        return null;
    }

    /**
     * 根据索引插入
     */
    public void insert(int index, int value) {
        Node pre = findNode(index - 1);//获得上一个节点
        if (pre == null) {
            throw new IllegalArgumentException("index is not legal");
        }
        Node next = pre.next;//获取下一个节点
        Node inserted = new Node(value, pre, next);//被插入的节点
        pre.next = inserted;//将上一个节点的next改为插入节点
        next.pre = inserted;//将下一个节点的pre改为插入节点
    }

    /**
     * 删除第一个节点
     * 不是删除头节点
     */
    public void removeFirst() {
        head.next = head.next.next;
        head.next.pre = head;
    }

    /**
     * 删除指定索引位置的元素
     *
     * @param index 索引
     */
    public void remove(int index) {
        Node pre = findNode(index - 1);
        Node node = findNode(index);
        if (node != null && pre != null) {
            pre.next = node.next;
            node.next.pre = pre;
        }
    }

    /**
     * while循环遍历
     */
    public void loop(Consumer<Integer> consumer) {
        Node node = head.next;
        while (node != tail) {
            consumer.accept(node.value);
            node = node.next;
        }
    }

    /**
     * for循环遍历
     */
    public void loop2(Consumer<Integer> consumer) {
        for (Node node = head.next; node != tail; node = node.next) {
            consumer.accept(node.value);
        }
    }

    /**
     * 递归遍历
     */
    public void loop3() {
        recursion(head.next);
    }

    /**
     * 递归方法
     */
    private void recursion(Node head) {
        if (head == tail) return;
        System.out.println(head.value);
        recursion(head.next);
    }

    /**
     * 迭代器遍历
     * 增强for
     */
    @Override
    public Iterator<Integer> iterator() {
        return getIterator();
    }

    private Iterator<Integer> getIterator() {
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }


}


